package com.avocado.base.controller;

import com.avocado.base.dto.resources.ResourcesAddDto;
import com.avocado.base.dto.resources.ResourcesEditDto;
import com.avocado.base.enums.ResourcesType;
import com.avocado.base.service.ResourcesService;
import com.avocado.base.vo.ResourcesAntdTree;
import com.avocado.commons.model.http.BaseResponse;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.security.Principal;
import java.util.List;





@Controller
@RequestMapping("/resources/*")
public class ResourcesController {
	

	 private final ResourcesService resourcesService;


	 public ResourcesController(ResourcesService resourcesService) {
		this.resourcesService = resourcesService;
	}

	 @PreAuthorize("hasAnyAuthority('RESOURCES_ADD')")
	 @RequestMapping("/add")
	 @CacheEvict(value="userMenu", allEntries=true)
     public @ResponseBody BaseResponse add(@RequestBody @Validated ResourcesAddDto t, Principal user){

		 long id=resourcesService.add(t);

		return BaseResponse.builder().status(1).data(id).message("操作成功!").build();
    }

	 @PreAuthorize("hasAnyAuthority('RESOURCES_UPDATE')")
	 @RequestMapping("/update")
	 //清空用户菜单缓存
	 @CacheEvict(value="userMenu", allEntries=true)
     public @ResponseBody BaseResponse update(@RequestBody  @Validated ResourcesEditDto t,Principal user){

		 resourcesService.update(t);

		 return BaseResponse.builder().status(1).message("操作成功!").build();
     }


	 @PreAuthorize("hasAnyAuthority('RESOURCES_DELETE')")
	 @RequestMapping("/deleteById")
	 @CacheEvict(value="userMenu", allEntries=true)
     public @ResponseBody BaseResponse deleteById(Long id) {

		 int status=resourcesService.deleteById(id)==Boolean.TRUE?1:0;

		 return BaseResponse.builder().status(status).message("操作成功!").build();

     }
	 
	 @RequestMapping("/getTree")
     public @ResponseBody BaseResponse getTree(ResourcesType resourcesType) {

		 return BaseResponse.builder().status(1).data(resourcesService.getTree(resourcesType)).build();
	 }

	 
	 /**
	  * 获取已登录用户对应的菜单
	  *
	  */
	 @RequestMapping("/getTreeForLoginUser")
	 //如果当前用户的菜单在缓存中已经存在，则需从缓存中取，而不是重新在数据库中获取
	 @Cacheable(value="userMenu",key="#user.name")
     public @ResponseBody BaseResponse getTreeForLoginUser(Principal user) {

		 List<ResourcesAntdTree> list=resourcesService.getTree(ResourcesType.MENU,user.getName());

		 return BaseResponse.builder().status(1).data(list).build();
     }



	@RequestMapping("/getCheckBoxTree")
	public @ResponseBody BaseResponse getCheckBoxTree(ResourcesType  resourcesType,Long roleId) {

		 List<ResourcesAntdTree> list=resourcesService.getCheckBoxTree(resourcesType,roleId);

		return BaseResponse.builder().status(1).data(list).build();
	}




}
